//帮派成员数据操作 {"对方数据":{"门派":"无门派","id":"46","申请时间":1747129784,"等级":1,"名称":"456456"},"类型":"同意加入"}
// @ts-ignore
export async function execute(client_id,message_json) {
    let characters = G.Characters.get_current_login_characters(client_id);
    let option_type = message_json["类型"];

    let gang_data = _GangDataCache.get(characters["BPID"]);
    if (gang_data === undefined) {
        ClientUtils.send_basic_prompt(client_id, "你还没有帮派或帮派已解散");
        return;
    }

    switch (option_type) {
        case "同意加入":
            approve_join(client_id, characters["id"], message_json["对方数据"]["id"], gang_data);
            break;
        case "任命职位":
            set_gang_member(client_id, characters, message_json["对方数据"]["id"], gang_data, message_json["职位"]);
            break;
        case "逐出帮派":
            remove_gang_member(client_id, characters, message_json["对方数据"]["id"], gang_data);
            break;
        default:
            break;
    }
}

function approve_join(client_id, characters_id, target_id, gang_data) {
    if (characters_id === target_id) {
        ClientUtils.send_basic_prompt(client_id, "不能对自己操作！");
    }
    if (gang_data["成员数据"][characters_id]["权限"] < 3) {
        ClientUtils.send_basic_prompt(client_id, "你没有权限执行此操作");
        return;
    }
    if (gang_data["成员数量"]["当前"] >= gang_data["成员数量"]["上限"]) {
        ClientUtils.send_basic_prompt(client_id, "帮派目前人数已达上限！");
        gang_data["申请人员"] = [];
        return;
    }
    if (!G.Characters.check_characters_online(target_id)) {
        ClientUtils.send_basic_prompt(client_id, "该玩家已经下线。");
        return;
    }
    let target_characters = _CharactersData.get(target_id);
    if (_GangDataCache.has(target_characters["BPID"])) {
        ClientUtils.send_basic_prompt(client_id, "对方已经加入了其他帮派！");
        return;
    }
    gang_data["成员数据"][target_id] = {
        "名称": target_characters["名称"],
        "门派": target_characters["门派"],
        "账号": target_characters["账号"],
        "等级": target_characters["等级"],
        "职务": "帮众",
        "权限": 1,
        "id": target_id,
        "帮贡": { 当前: 0, 上限: 20000 },
        "青龙": 0,
        "跑商": 0,
        "迷宫": 0,
        "厢房": 0,
        "帮战": 0,
        "离线时间": Math.floor(Date.now() / 1000),
        "在线": true
    };
    target_characters["BPMC"] = gang_data["帮派名称"];
    target_characters["BPID"] = gang_data["帮派编号"];
    gang_data["成员数量"]["当前"] += 1;
    for (let index = 0; index < gang_data["申请人员"].length; index++) {
        const member = gang_data["申请人员"][index];
        if (member["id"] === target_characters["id"]) {
            gang_data["申请人员"].splice(index, 1);
            break;
        }
    }
    let target_client_id = G.Characters.get_characters_client_id(target_id);
    if (target_client_id !== undefined) {
        ClientUtils.send_client_message(target_client_id, 246, gang_data["帮派名称"]);
        ClientUtils.send_client_message(target_client_id, 7, "#Y/恭喜你已经被批准加入帮派,赶快和帮派里的小伙伴打声招呼吧。");
        //添加称谓
        G.Characters.add_chengwei(target_id,`${gang_data["帮派名称"]}帮众`);
    }
    ClientUtils.send_client_message(client_id, 208, {
        "成员数据": gang_data["成员数据"],
        "成员数量": gang_data["成员数量"]["当前"],
        "类型": "任命职位",
    });
    ClientUtils.send_basic_prompt(client_id, "操作成功。");
    G.GangUtils.notice_gang_chat(gang_data["帮派编号"], `[帮派总管]#W/恭喜新朋友#G/${target_characters["名称"]}#W/加入${gang_data["帮派名称"]}大家庭！`);
}

function remove_gang_member(client_id, characters, target_id, gang_data) {
    if (characters["id"] === target_id) {
        ClientUtils.send_basic_prompt(client_id, "不能对自己操作！");
    }
    if (gang_data["成员数据"][characters["id"]]["权限"] < 3) {
        ClientUtils.send_basic_prompt(client_id, "你没有权限执行此操作");
        return;
    }
    let target_characters = gang_data["成员数据"][target_id];
    if (target_characters !== undefined) {
        let target_characters = _CharactersData.get(target_id);
        if (target_characters !== undefined) {
            target_characters["BPMC"] = 0;
            target_characters["BPID"] = "无帮派";
            let client_id = G.Characters.get_characters_client_id(target_id);
            if (client_id !== undefined) {
                ClientUtils.send_basic_prompt(client_id, "你已被逐出帮派");
                //删除帮派称谓
                G.Characters.remove_chengwei(target_id, `${gang_data["帮派名称"]}${gang_data["成员数据"][target_id]["职务"]}`);
            }
        }
        ClientUtils.send_basic_prompt(client_id, "操作成功");
        G.GangUtils.notice_gang_chat(gang_data["帮派编号"], `[帮派总管]#G/ ${characters["名称"]} #Y/将${target_characters["名称"]} #Y/逐出了帮派！`)
        gang_data["成员数量"]["当前"] -= 1;
        gang_data["成员数据"][target_id] = undefined;
        ClientUtils.send_client_message(client_id, 208, {
            "成员数据": gang_data["成员数据"],
            "成员数量": gang_data["成员数量"]["当前"],
            "类型": "逐出帮派",
        });
    }
}

function set_gang_member(client_id, characters, target_id, gang_data, post) {
    if (characters["id"] === target_id) {
        ClientUtils.send_basic_prompt(client_id, "不能对自己操作！");
    }
    if (gang_data["成员数据"][characters["id"]]["权限"] !== G.GangUtils.gang_post_level["帮主"]) {
        ClientUtils.send_basic_prompt(client_id, "你没有权限执行此操作");
        return;
    }
    let post_level = G.GangUtils.gang_post_level[post];
    if (post_level === undefined) {
        ClientUtils.send_basic_prompt(client_id, "职位不存在");
        return;
    }
    let gang_target_characters = gang_data["成员数据"][target_id];
    if (gang_target_characters === undefined) {
        ClientUtils.send_basic_prompt(client_id, "成员不存在");
        return;
    }
    if (post === gang_target_characters["职务"]) {
        ClientUtils.send_basic_prompt(client_id, "请勿重复任命");
        return;
    }

    let old_post = gang_data["成员数据"][characters["id"]]["职务"];
    let tagret_old_post = gang_target_characters["职务"];
    //转让帮主
    if (post === "帮主") {
        gang_data["现任帮主"] = { "名称": gang_target_characters["名称"], "id": target_id };
        gang_data["成员数据"][characters["id"]]["职务"] = "副帮主";
        gang_data["成员数据"][characters["id"]]["权限"] = G.GangUtils.gang_post_level["副帮主"];
        // 更新原帮主的职务称谓
        G.Characters.remove_chengwei(characters["id"], `${gang_data["帮派名称"]}${old_post}`);
        G.Characters.add_chengwei(characters["id"], `${gang_data["帮派名称"]}副帮主`);
        G.GangUtils.notice_gang_chat(gang_data["帮派编号"], `[帮派总管]#G/ ${characters["名称"]} #W将帮派转让给了#G${gang_target_characters["名称"]}#W！`);
    } else if (post === "副帮主") {
        G.GangUtils.notice_gang_chat(gang_data["帮派编号"], `[帮派总管]#G/ ${characters["名称"]} #W将#G${gang_target_characters["名称"]}#W委任为帮派的#R副帮主#W！`);
    } else {
        G.GangUtils.notice_gang_chat(gang_data["帮派编号"], `[帮派总管]#G/ ${characters["名称"]} #W将#G${gang_target_characters["名称"]}#W提拔为帮派#R${post}#W！`);
    }
    gang_target_characters["职务"] = post;
    gang_target_characters["权限"] = post_level;

    ClientUtils.send_client_message(client_id, 208, {
        "成员数据": gang_data["成员数据"],
        "成员数量": gang_data["成员数量"]["当前"],
        "类型": "任命职位",
    });

    let target_characters = _CharactersData.get(target_id);
    if (target_characters !== undefined) {
        let target_client_id = G.Characters.get_characters_client_id(target_id);
        if (target_client_id !== undefined) {
            ClientUtils.send_client_message(target_client_id, 7, "你的帮派帮派职位发生了变更！");
            // 更新职务称谓
            G.Characters.remove_chengwei(target_id, `${gang_data["帮派名称"]}${tagret_old_post}`);
            G.Characters.add_chengwei(target_id, `${gang_data["帮派名称"]}${post}`);
        }
    }
    ClientUtils.send_basic_prompt(client_id, "任命成功！");
}